package com.org.devg.monitor.server.api.service;

import com.org.devg.monitor.server.model.MonitorServerConfig;
import org.devgframwork.platform.model.ResultModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
public class WeChatMessageService {

    @Autowired
    private MonitorServerConfig monitorServerConfig;

    public ResultModel<String> send(String textMsg) throws IOException {
        CloseableHttpClient httpClient = null;
        boolean sendSuccess = false;
        String result = null;
        int statusCode = 0;
        try {
            httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(monitorServerConfig.getWechatHookUrl());
            httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
            StringEntity se = new StringEntity(textMsg, "utf-8");
            httpPost.setEntity(se);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
                sendSuccess = true;
            } else {
                statusCode = response.getStatusLine().getStatusCode();
            }
            response.close();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            result = ex.getMessage();
        } finally {
            if (null != httpClient) {
                httpClient.close();
            }
        }
        if (sendSuccess) {
            return ResultModel.newSuccess(result);
        }
        ResultModel<String> resultModel = ResultModel.newFail();
        resultModel.setCode(statusCode);
        return resultModel;
    }

    public ResultModel<String> sendText(String content) throws IOException {
        content = "{\n" +
                "  \"msgtype\": \"text\",\n" +
                "  \"text\": {\n" +
                "    \"content\": \"" + content + "\"\n" +
                "  }\n" +
                "}";
        return send(content);
    }

 /*   restTemplate 用法
      // 请求头设置,x-www-form-urlencoded格式的数据
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
      //提交参数设置
      MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
      String wechatMsg = "方法名:" + monitorMessage.getClassMethod() + ",消息KEY:" + monitorMessage.getMessageKey() + ",执行时长:" + monitorMessage.getCostTime() + "ms,请求时间:" + monitorMessage.getStartTime();
      map.add("msg", wechatMsg);
      // 组装请求体
      HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
      ResponseEntity<String> entity = restTemplate.postForEntity(wechatMessageUrl, request, String.class);
      log.info("send message result:{}", entity.getBody());
 */

}
